<?php

namespace app\bis\controller;

use think\Request;

class Location extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $validate = validate('BisLocation');
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            //分解组合分类信息
            if (!empty($data['se_category_id'])) {
                $data['cate'] = implode(',', $data['se_category_id']);
            }
            //验证通过数据入库
            $LocationData = [
                'name' => $data['name'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
                'logo' => $data['logo'],
                'category_id' => empty($data['se_category_id']) ? $data['category_id'] : $data['category_id'] . ',' . $data['cate'],
                'api_address' => $data['address'],
                'tel' => $data['tel'],
                'contact' => empty($data['contact']) ? '' : $data['contact'],
                'open_time' => empty($data['open_time']) ? '' : $data['open_time']
            ];
            $res = model('BisLocation')->add($LocationData);
            if ($res) {
                $this->success('添加成功', url('index/index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            //获取城市信息
            $citys = model('city')->getNormalCitysByParentId();
            //获取分类信息
            $categorys = model('category')->getNormalFirstCategory();
            return $this->fetch('', ['citys' => $citys, 'categorys' => $categorys]);
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function index()
    {
        //显示店铺信息
        $location = model('bisLocation')->getLocation();
        return $this->fetch('', ['location' => $location]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
